﻿/*----------------------------------------------------------------
 * 版权所有 (c) 2025 HaiTangYunchi  保留所有权利
 * CLR版本：4.0.30319.42000
 * 公司名称：
 * 命名空间：SalaryCalculator.Repositories
 * 唯一标识：592b59f6-a768-4f0e-86e2-3d841316880e
 * 文件名：SettlementRecordRepository
 * 
 * 创建者：海棠云螭
 * 电子邮箱：haitangyunchi@126.com
 * 创建时间：2025/5/10 20:19:55
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;

namespace SalaryCalculator
{
    /// <summary>
    /// 结算记录数据访问类
    /// </summary>
    public class SettlementRecordRepository
    {
        /// <summary>
        /// 获取所有结算记录
        /// </summary>
        public List<SettlementRecord> GetAllSettlementRecords()
        {
            var records = new List<SettlementRecord>();

            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = @"
                SELECT sr.*, e.Name AS EmployeeName 
                FROM SettlementRecords sr
                JOIN Employees e ON sr.EmployeeId = e.Id
                ORDER BY sr.SettlementDate DESC";

                using (var command = new SQLiteCommand(query, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            records.Add(new SettlementRecord
                            {
                                Id = Convert.ToInt32(reader["Id"]),
                                EmployeeId = Convert.ToInt32(reader["EmployeeId"]),
                                StartDate = DateTime.Parse(reader["StartDate"].ToString()),
                                EndDate = DateTime.Parse(reader["EndDate"].ToString()),
                                SettlementDate = DateTime.Parse(reader["SettlementDate"].ToString()),
                                TotalHours = Convert.ToDouble(reader["TotalHours"]),
                                TotalAmount = Convert.ToDecimal(reader["TotalAmount"]),
                                EmployeeName = reader["EmployeeName"].ToString()
                            });
                        }
                    }
                }
            }

            return records;
        }

        /// <summary>
        /// 添加结算记录
        /// </summary>
        public void AddSettlementRecord(SettlementRecord record)
        {
            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = @"
                INSERT INTO SettlementRecords 
                (EmployeeId, StartDate, EndDate, SettlementDate, TotalHours, TotalAmount)
                VALUES (@EmployeeId, @StartDate, @EndDate, @SettlementDate, @TotalHours, @TotalAmount)";

                using (var command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@EmployeeId", record.EmployeeId);
                    command.Parameters.AddWithValue("@StartDate", record.StartDate.ToString("yyyy-MM-dd"));
                    command.Parameters.AddWithValue("@EndDate", record.EndDate.ToString("yyyy-MM-dd"));
                    command.Parameters.AddWithValue("@SettlementDate", record.SettlementDate.ToString("yyyy-MM-dd"));
                    command.Parameters.AddWithValue("@TotalHours", record.TotalHours);
                    command.Parameters.AddWithValue("@TotalAmount", record.TotalAmount);

                    command.ExecuteNonQuery();
                }
            }
        }
    }
}
